package com.hopster;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.util.Log;

public class AlarmService
{
	private static final AlarmService instance = new AlarmService();
	private static int intentID = 0;
	
	private List<Alarm> alarms;
	
    // Private constructor prevents instantiation from other classes
    private AlarmService()
    {
    	// TODO: Initialize list from local storage.
    	alarms = new ArrayList<Alarm>();
    }
    
    /**
     * Gets the current (and only) instance of AlarmService.
     * @return The only AlarmService object.
     */
    public static AlarmService getInstance()
    {
    	return instance;
    }
    
    /**
     * Wrapper for addAlarm(alarm, true)
     */
    public boolean addAlarmRecur(Alarm alarm)
    {
    	return addAlarm(alarm, true);
    }
    
    /**
     * Wrapper for addAlarm(alarm, false);
     */
    public boolean addAlarm(Alarm alarm)
    {
    	return addAlarm(alarm, false);
    }
    
    /**
     * Wrapper for addAlarm(new Alarm(name, hour, minute))
     */
    public boolean addAlarm(String name, int hour, int minute)
    {
    	Alarm alarm = new Alarm(name, hour, minute);
    	return addAlarm(alarm);
    }
    
    /**
     * Attempts to add the given alarm to the list of current alarms. If recurring is
     * true, it will set the alarm to repeat once a day.
     * @param alarm Alarm to add.
     * @param recurring True if the alarm repeats everyday, false if it's a one-shot alarm.
     * @return False if an alarm with the same name already exists, true otherwise.
     */
    public boolean addAlarm(Alarm alarm, boolean recurring)
    {
    	if(alarms.contains(alarm))
    	{
    		return false;
    	}
    	else
    	{
    		boolean success = alarms.add(alarm);
    		
    		if(success)
    		{
    			Calendar time = Calendar.getInstance();
    			time.setTimeInMillis(System.currentTimeMillis());
    			time.set(Calendar.MINUTE, alarm.getMinute());
    			time.set(Calendar.HOUR_OF_DAY, alarm.getHour());
    			time.set(Calendar.SECOND, 0);
    			
    			Log.w("AlarmService", "Setting an alarm for: " + time.getTime().toString());
    			
    			PendingIntent pendingIntent = alarm.getIntent();
    			
    			AlarmManager am = (AlarmManager) HopsterActivity.context.getSystemService(Context.ALARM_SERVICE);
    			
    			if(recurring)
    			{
        			am.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent);
    			}
    			else
    			{
        			am.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    			}
    		}
    		
    		return success;
    	}
    }
    
    /**
     * Attempts to remove the alarm with the given name from the list of alarms.
     * @param alarm Alarm to remove.
     * @return True if successful, false otherwise.
     */
    public boolean removeAlarm(Alarm alarm)
    {
    	boolean success = alarms.remove(alarm);
    	
    	if(success)
    	{
    		PendingIntent intent = alarm.getIntent();
    		if(intent != null)
    		{
    			AlarmManager am = (AlarmManager) HopsterActivity.context.getSystemService(Context.ALARM_SERVICE);
    			am.cancel(intent);
    		}
    	}
    	
    	return success;
    }
    
    public boolean removeAlarm(String alarmName)
    {
    	boolean success = false;
    	for(int k=0; k<alarms.size(); k++)
    	{
    		Alarm a = alarms.get(k);
    		if(a.getName().equals(alarmName))
    		{
    			return removeAlarm(a);
    		}
    	}
    	return success;
    	//return alarms.remove(new Alarm(alarmName, 0, 0));
    }
    
    public static int getNewID()
    {
    	intentID++;
    	return intentID;
    }
    
    public List<Alarm> getAlarms()
    {
    	return alarms;
    }
    
    public boolean matchesAlarms(List<Alarm> list)
    {
    	return list.equals(alarms);
    }
}

